using Digitalmes.Application.Sys.Users.Queries;

namespace Digitalmes.WebApi.Endpoints;

/// <summary>
/// 用户信息终结点。
/// </summary>
public sealed class UserEndpoint : EndpointGroupBase
{
    public override void Map(WebApplication app)
    {
        app.MapGroup(this)
           .RequireAuthorization()
           .MapGet(GetCurrentUserInfoAsync, "info");
    }

    [EndpointDescription("获取当前用户信息")]
    public async Task<IApiResult> GetCurrentUserInfoAsync(ISender sender, ICurrentUser currentUser)
    {
        var user = (await sender.Send(new GetUserByIdQuery(currentUser.UserId ?? 0))).Value;
        var userInfo = new
        {
            userId = currentUser.UserId,
            username = currentUser.Username,
            realName = user?.Realname,
            avatar = user?.Avatar,
            roles = currentUser.Roles,
            desc = user?.Remark,
        };

        return ApiResult.Ok(userInfo);
    }
}
